<template>
  <view class="table-brand-container">
    <!-- 区域列表 -->
    <view class="brand-list">
      <!-- 列表项 -->
      <view class="brand-item" v-for="zone in zones" :key="zone.id" @click="handleZoneClick(zone)">
        <view class="brand-info">
          <text class="brand-name">{{ zone.name }}</text>
          <text class="brand-weight">权重: {{ zone.weigh }}</text>
        </view>
        <view class="brand-action">
          <text class="brand-time">{{ formatDate(zone.createtime) }}</text>
          <view class="action-buttons">
            <view class="edit-btn" @click.stop="handleEdit(zone)">
              <image src="/static/xiugai07_1.png" mode="aspectFit" class="btn-icon"></image>
            </view>
            <view class="delete-btn" @click.stop="handleDelete(zone)">
              <image src="/static/shanchu_1.png" mode="aspectFit" class="btn-icon"></image>
            </view>
          </view>
        </view>
      </view>
      
      <!-- 空状态 -->
      <view class="empty-state" v-if="zones.length === 0 && !loading">
        <image src="/static/empty.png" mode="aspectFit" class="empty-icon"></image>
        <text class="empty-text">暂无区域信息</text>
      </view>
    </view>
    
    <!-- 加载状态 -->
    <view class="loading-state" v-if="loading">
      <view class="loading-spinner"></view>
      <text class="loading-text">加载中...</text>
    </view>
    
    <!-- 分页 -->
    <view class="pagination" v-if="zones.length > 0 && !loading">
      <button class="page-btn" :disabled="currentPage <= 1" @click="changePage(currentPage - 1)">上一页</button>
      <text class="page-info">{{ currentPage }}/{{ totalPages }}</text>
      <button class="page-btn" :disabled="currentPage >= totalPages" @click="changePage(currentPage + 1)">下一页</button>
    </view>
    
    <!-- 添加按钮 -->
    <view class="add-btn" @click="handleAdd">
      <image src="/static/tianjia_2.png" mode="aspectFit" class="add-icon"></image>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      zones: [],
      loading: false,
      currentPage: 1,
      pageSize: 10,
      totalPages: 1,
      totalCount: 0
    };
  },
  onShow() {
    this.getZoneList();
  },
  methods: {
    // 获取区域列表
    async getZoneList() {
      try {
        this.loading = true;
        const res = await this.$request({
          url: '/table/zone/index',
          method: 'GET',
          params: {
            page: this.currentPage,
            limit: this.pageSize
          }
        });
        
        if (res.code === 1) {
          this.zones = res.data.rows || [];
          this.totalCount = res.data.total || 0;
          this.totalPages = Math.ceil(this.totalCount / this.pageSize);
        } else {
          throw new Error(res.msg || '获取区域列表失败');
        }
      } catch (error) {
        console.error('获取区域列表失败:', error);
        uni.showToast({
          title: error.message || '获取区域列表失败',
          icon: 'none'
        });
      } finally {
        this.loading = false;
      }
    },
    
    // 格式化日期
    formatDate(dateString) {
      if (!dateString) return '';
      const date = new Date(dateString);
      return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')}`;
    },
    
    // 切换分页
    changePage(page) {
      if (page < 1 || page > this.totalPages) return;
      this.currentPage = page;
      this.getZoneList();
    },
    
    // 点击区域查看详情
    handleZoneClick(zone) {
      uni.navigateTo({
        url: `/pages/tablezone/components/zonedetail?id=${zone.id}`
      });
    },
    
    // 编辑区域
    handleEdit(zone) {
      uni.navigateTo({
        url: `/pages/tablezone/components/zoneedit?id=${zone.id}`
      });
    },
    
    // 添加区域
    handleAdd() {
      uni.navigateTo({
        url: '/pages/tablezone/components/zoneedit'
      });
    },
    
    // 删除区域
    handleDelete(zone) {
      uni.showModal({
        title: '确认删除',
        content: `确定要删除区域 "${zone.name}" 吗？`,
        success: async (res) => {
          if (res.confirm) {
            try {
              const result = await this.$request({
                url: '/table/zone/del',
                method: 'POST',
                data: {
                  ids: zone.id
                }
              });
              
              if (result.code === 1) {
                uni.showToast({
                  title: '删除成功',
                  icon: 'success'
                });
                // 重新获取区域列表
                this.getZoneList();
              } else {
                throw new Error(result.msg || '删除失败');
              }
            } catch (error) {
              console.error('删除区域失败:', error);
              uni.showToast({
                title: error.message || '删除失败',
                icon: 'none'
              });
            }
          }
        }
      });
    }
  }
};
</script>

<style scoped>
.table-brand-container {
  min-height: 100vh;
  background-color: #f8f8f8;
  padding: 20rpx;
  padding-bottom: 120rpx;
}

/* 区域列表 */
.brand-list {
  width: 100%;
}

/* 区域列表项 */
.brand-item {
  background-color: #fff;
  border-radius: 15rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.brand-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15rpx;
}

.brand-name {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.brand-weight {
  font-size: 24rpx;
  color: #666;
  background-color: #f0f0f0;
  padding: 4rpx 16rpx;
  border-radius: 20rpx;
}

.brand-action {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 20rpx;
  border-top: 1rpx solid #eee;
}

.brand-time {
  font-size: 24rpx;
  color: #999;
}

.action-buttons {
  display: flex;
  gap: 20rpx;
}

.btn-icon {
  width: 40rpx;
  height: 40rpx;
}

/* 空状态 */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 100rpx 0;
}

.empty-icon {
  width: 200rpx;
  height: 200rpx;
  margin-bottom: 30rpx;
  opacity: 0.5;
}

.empty-text {
  font-size: 28rpx;
  color: #999;
}

/* 加载状态 */
.loading-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 100rpx 0;
}

.loading-spinner {
  width: 50rpx;
  height: 50rpx;
  border: 6rpx solid #f3f3f3;
  border-top: 6rpx solid #4cd964;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 20rpx;
}

.loading-text {
  font-size: 28rpx;
  color: #666;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 分页 */
.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 40rpx;
  padding: 30rpx;
  background-color: #fff;
  margin-top: 20rpx;
  border-radius: 15rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.page-btn {
  padding: 12rpx 40rpx;
  background-color: #4cd964;
  color: #fff;
  border: none;
  border-radius: 30rpx;
  font-size: 28rpx;
}

.page-btn:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}

.page-info {
  font-size: 28rpx;
  color: #666;
}

/* 添加按钮 */
.add-btn {
  position: fixed;
  bottom: 40rpx;
  right: 40rpx;
  width: 100rpx;
  height: 100rpx;
  border-radius: 50%;
  background-color: #4cd964;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4rpx 20rpx rgba(76, 217, 100, 0.4);
}

.add-icon {
  width: 50rpx;
  height: 50rpx;
}
</style>